﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;


namespace CharityGamesOnline.Modules.Event
{
    public partial class EventSetting : ModuleSettingsBase
    {
        private string CultureCode
        {
            get { return LocaleController.Instance.GetCurrentLocale(PortalId).Code; }
        }
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// LoadSettings loads the settings from the Database and displays them 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    if (!Null.IsNull(TabModuleSettings["ViewMode"]) && !string.IsNullOrEmpty((string)TabModuleSettings["ViewMode"]))
                    {
                        ddlViewMode.SelectedIndex = ddlViewMode.Items.IndexOf(ddlViewMode.Items.FindByValue((string)TabModuleSettings["ViewMode"]));
                    }

                    if (!Null.IsNull(TabModuleSettings["ShowArchive"]) && !string.IsNullOrEmpty((string)TabModuleSettings["ShowArchive"]))
                    {
                        cbShowArchive.Checked = Boolean.Parse(TabModuleSettings["ShowArchive"].ToString());
                    }

                    List<TabInfo> listTabs = TabController.GetPortalTabs(TabController.GetTabsBySortOrder(PortalId, CultureCode, true), Null.NullInteger, true, "<" + Localization.GetString("None_Specified") + ">", true, false, false, false, false);
                    ddlCharityDetailTabID.DataSource = listTabs;
                    ddlCharityDetailTabID.DataBind();

                    ddlEventDetailTabID.DataSource = listTabs;
                    ddlEventDetailTabID.DataBind();

                    ddlArchiveDetailTabID.DataSource = listTabs;
                    ddlArchiveDetailTabID.DataBind();

                    ddlGameDetailTabID.DataSource = listTabs;
                    ddlGameDetailTabID.DataBind();

                    if (!Null.IsNull(ModuleSettings["CharityDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["CharityDetailTabID"]))
                    {
                        ListItem selItem = ddlCharityDetailTabID.Items.FindByValue(ModuleSettings["CharityDetailTabID"].ToString());
                        if (selItem != null)
                            selItem.Selected = true;
                    }

                    if (!Null.IsNull(ModuleSettings["EventDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["EventDetailTabID"]))
                    {
                        ListItem selItem = ddlEventDetailTabID.Items.FindByValue(ModuleSettings["EventDetailTabID"].ToString());
                        if (selItem != null)
                            selItem.Selected = true;
                    }

                    if (!Null.IsNull(ModuleSettings["ArchiveDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["ArchiveDetailTabID"]))
                    {
                        ListItem selItem = ddlArchiveDetailTabID.Items.FindByValue(ModuleSettings["ArchiveDetailTabID"].ToString());
                        if (selItem != null)
                            selItem.Selected = true;
                    }

                    if (!Null.IsNull(ModuleSettings["GameDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["GameDetailTabID"]))
                    {
                        ListItem selItem = ddlGameDetailTabID.Items.FindByValue(ModuleSettings["GameDetailTabID"].ToString());
                        if (selItem != null)
                            selItem.Selected = true;
                    }
                }
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// UpdateSettings saves the modified settings to the Database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public override void UpdateSettings()
        {
            try
            {
                ModuleController objModules = new ModuleController();

                objModules.UpdateTabModuleSetting(TabModuleId, "ViewMode", ddlViewMode.SelectedValue);
                objModules.UpdateTabModuleSetting(TabModuleId, "ShowArchive", cbShowArchive.Checked.ToString());
                
                objModules.UpdateModuleSetting(ModuleId, "CharityDetailTabID", ddlCharityDetailTabID.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "EventDetailTabID", ddlEventDetailTabID.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "ArchiveDetailTabID", ddlArchiveDetailTabID.SelectedValue);                
                objModules.UpdateModuleSetting(ModuleId, "GameDetailTabID", ddlGameDetailTabID.SelectedValue);
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #region Private Function
        private void LoadPages()
        {
 
        }
        #endregion
    }
}